package com.blog.domain;

import java.util.HashMap;
import java.util.Map;

public class User extends POJO {

	private static final long serialVersionUID = -6779017414773342895L;

	// content
	protected String userName;
	protected String userPwd;

	private static final Map<String, User> USERS = new HashMap<String, User>();

	public User(String name,String passwd){
		this.userName = name;
		this.userPwd = passwd;
	}
	
	@Override
	protected String getTable() {
		return null;
	}

	public boolean valid() {
		return USERS.containsKey(this.getUserName())
				&& USERS.get(this.getUserName()).equals(this);
	}

	public static User getUserByName( String name ){
		return USERS.get(name);
	}
	
	public static void init(String name, String pwd) {
		User u = new User(name,pwd);
		if( !USERS.containsKey(name) ){
			USERS.put(name, u);
		}
	}

	public String getUserName() {
		return userName;
	}

	public void setUserName(String userName) {
		this.userName = userName;
	}

	public String getUserPwd() {
		return userPwd;
	}

	public void setUserPwd(String userPwd) {
		this.userPwd = userPwd;
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = super.hashCode();
		result = prime * result
				+ ((userName == null) ? 0 : userName.hashCode());
		result = prime * result + ((userPwd == null) ? 0 : userPwd.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (!super.equals(obj))
			return false;
		if (getClass() != obj.getClass())
			return false;
		User other = (User) obj;
		if (userName == null) {
			if (other.userName != null)
				return false;
		} else if (!userName.equals(other.userName))
			return false;
		if (userPwd == null) {
			if (other.userPwd != null)
				return false;
		} else if (!userPwd.equals(other.userPwd))
			return false;
		return true;
	}

}
